package main;

import usersObjects.*;
import systemMethods.*;
import systemActions.*;
import serializableObjects.*;

public class UsersManager {

    private static void startSystem2() {
        String answerFromConsole;
        int numberOfUserAction;

        while (true) {
            System.out.println("Please, enter the number of the required actions");
            System.out.println("1) Sign up a new user or Sign in the existing user");
            System.out.println("2) Logout");

            answerFromConsole = TestingSystemUtils.readFromConsole();

            try {
                numberOfUserAction = Integer.parseInt(answerFromConsole);

                switch (numberOfUserAction) {
                    case 1:
                        SignUpOrIn();
                        break;
                    case 2:
                        LogOut();
                        break;
                    default:
                        System.out.println("You have done a wrong choice");
                        break;
                }

            } catch (NumberFormatException e) {
                System.out.println("Please, try to type a correct number");
            }
        }
    }

    private static void LogOut() {
        TestingSystemUtils.getOutFromProgram("Exit from the program ...", 0);
    }

    private static void SignUpOrIn() {
        String userLogin;
        String userPassword;
        UserCertificate currentUserCertificate;
        StorageOfUsers currentStorageOfUser;

        System.out.println("Please, type a login for a user");
        userLogin = TestingSystemUtils.readFromConsole();
        System.out.println("Please, type a password for a user");
        userPassword = TestingSystemUtils.readFromConsole();

        currentUserCertificate = new UserCertificate(userLogin, userPassword);
        currentStorageOfUser = SerializableMethods.loadUsers();

        SystemAction currentSystemAction = SystemActionsFactory.getBaseComponent("UserWorkbench");
        currentSystemAction = SystemActionsFactory.wrapWithAction("PreparationOfQuestions", currentSystemAction);
        currentSystemAction = SystemActionsFactory.wrapWithAction("UsersRegistration", currentSystemAction);
        currentSystemAction.executeAction(currentStorageOfUser, currentUserCertificate);
    }

    public static void main(String[] args) {
        startSystem2();
    }
}
